package com.cskaoyan.service;

import com.cskaoyan.bean.Comment;
import com.cskaoyan.bean.CommentExample;
import com.cskaoyan.bean.comment.CommentListVO;
import com.cskaoyan.bean.comment.OrderReplyBO;
import com.cskaoyan.bean.goods.CommentListBO;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.until.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentMapper commentMapper;

    /**
     * 获取评论列表
     * 涉及表格：comment
     * @param baseParam
     * @param commentListBO
     * @return
     */
    @Override
    public CommentListVO queryComments(BaseParam baseParam, CommentListBO commentListBO) {

        //查询条件及分页设置
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria commentExampleCriteria = commentExample.createCriteria();
        if (commentListBO.getUserId() != null) {
            commentExampleCriteria.andUserIdEqualTo(commentListBO.getUserId());
        }
        if (commentListBO.getValueId() != null) {
            commentExampleCriteria.andValueIdEqualTo(commentListBO.getValueId());
        }
        commentExample.setOrderByClause(baseParam.getSort(), baseParam.getOrder());
        commentExampleCriteria.andDeletedEqualTo(false);
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());

        //查询；
        List<Comment> comments = commentMapper.selectByExample(commentExample);

        PageInfo<Comment> commentPageInfo = new PageInfo<>();
        long total = commentPageInfo.getTotal();
        int pages = commentPageInfo.getPages();

        CommentListVO commentListVO = new CommentListVO();
        commentListVO.setLimit(baseParam.getLimit());
        commentListVO.setList(comments);
        commentListVO.setPage(baseParam.getPage());
        commentListVO.setPages(pages);
        commentListVO.setTotal(total);

        return commentListVO;
    }

    /**
     * 删除评论
     * 涉及表格：comment
     * @param comment
     * @return
     */
    @Override
    public Integer deleteComment(Comment comment) {
        comment.setUpdateTime(new Date());
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria commentExampleCriteria = commentExample.createCriteria();

        commentExampleCriteria.andIdEqualTo(comment.getId());
        commentExampleCriteria.andDeletedEqualTo(false);

        comment.setDeleted(true);
        int affectedRows = commentMapper.updateByExampleSelective(comment, commentExample);

        return affectedRows;
    }

    /**
     * 回复商品评论
     * 涉及表格：comment
     * @param orderReplyBO
     * @return
     */
    @Override
    public Integer replyComment(OrderReplyBO orderReplyBO) {
        Integer commentId = orderReplyBO.getCommentId();
        String content = orderReplyBO.getContent();

        Comment comment = commentMapper.selectByPrimaryKey(commentId);
        if (StringUtils.isEmpty(comment.getAdminContent().trim())) {
            Date date = new Date();
            CommentExample commentExample = new CommentExample();
            CommentExample.Criteria commentExampleCriteria = commentExample.createCriteria();
            commentExampleCriteria.andDeletedEqualTo(false);
            commentExampleCriteria.andIdEqualTo(commentId);
            comment.setUpdateTime(date);
            comment.setAdminContent(content);

            int afectedRows = commentMapper.updateByExampleSelective(comment, commentExample);

            return afectedRows;
        }

        return 0;
    }


}
